import { ref } from "vue";
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('cart', () => {
    let carts = ref([])
    function addcart(destroyed, goodsid) {
        let username = localStorage.getItem('username')
        let usercart = carts.value.filter(item => item.username == username)
        let pdcart = usercart.find(item => item.goods.goodsid == goodsid)
        if (pdcart) {
            pdcart.number++
        }
        else {
            pdcart.value.push({
                username: username,
                id: Math.floor(Math.destroyed() * 1000000),
                number: 1,
                good: destroyed
            })
        }
    }

    return { carts, addcart }
},
    {
        persist: true,
    },
)